<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ MCP SDK/Working with Resources</title>
        <meta name="description" content="A lightweight Java 8 Model Context Protocol (MCP) SDK. Working with Resources"/>
        <meta name="keywords" content="mcp, mcp sdk, java mcp, mcp client, mcp server, java8 mcp, Resources"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" href="/asset/style/main.css"/>
        <link rel="icon" type="image/png" href="/asset/favicon.ico"/>
        <script src="/asset/common.js"></script>
        <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@200..900&family=Noto+Serif+TC:wght@200..900&family=Noto+Serif:ital,wght@0,100..900;1,100..900&family=Open+Sans:ital,wght@0,300..800;1,300..800&display=swap" rel="stylesheet" />
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/">🏠 Home</a>
                    | ⚙️ Source:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-mcp">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-mcp">Gitcode</a>
                    |
                    <a href="/cn">Chinese Version</a>
                </div>
                <h1>AJ MCP SDK</h1>
                <h3>User Manual</h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/">Home</a>
                    </li>
                    <li>
                        <a href="/architecture/index">Architecture</a>
                    </li>
                </ul>
                <h3>MCP Client SDK</h3>
                <ul>
                    <li>
                        <a href="/client/setup-client">Client SDK Setup</a>
                    </li>
                    <li>
                       <a href="/client/initialization">Initialization</a>
                    </li>
                    <li>
                       <a href="/client/resource">Resources Usage</a>
                    </li>
                     <li>
                       <a href="/client/prompt">Prompts Usage</a>
                    </li>
                     <li>
                       <a href="/client/tool">Tools Usage</a>
                    </li>
                </ul>
                <h3>MCP Server SDK</h3>
                <ul>
                      <li>
                           <a href="/server/usage-server">Server SDK Usage</a>
                      </li>
                      <li>
                           <a href="/server/res-server">Resources Usage</a>
                      </li>
                      <li>
                           <a href="/server/pro-server">Prompts Usage</a>
                      </li>
                      <li>
                             <a href="/server/tool-server">Tools Usage</a>
                       </li>
                      <li>
                         <a href="/server/sample">Integration Samples</a>
                      </li>
                </ul>

                <h3>Misc</h3>
                <ul>
                    <li><a href="/misc/versions">Release History</a></li>
                    <li><a href="/misc/contact">Contact</a></li>
                </ul>
            </menu>
            <article>
                <h2>Working with Resources</h2>
<p>The resource system enables LLM-powered applications to access structured content through a unified interface.
Resources are identified by URIs and can represent any type of content that might be useful for an LLM to process.
The AJ MCP client provides methods for discovering and accessing resources exposed by the server.</p>
<h3>Listing Resources</h3>
<p>To list available resources:</p>
<pre><code class="language-java">List&lt;ResourceItem&gt; resourceList = mcpClient.listResources();
assertEquals(2, resourceList.size(), &quot;Expected exactly two resources&quot;);

ResourceItem blob = getResourceRef(&quot;blob&quot;, resourceList);
assertNotNull(blob, &quot;Resource 'blob' should not be null&quot;);
assertEquals(&quot;blob&quot;, blob.getName(), &quot;Name mismatch for 'blob'&quot;);
assertEquals(&quot;file:///blob&quot;, blob.getUri(), &quot;URI mismatch for 'blob'&quot;);
assertEquals(&quot;image/jpg&quot;, blob.getMimeType(), &quot;MIME type mismatch for 'blob'&quot;);
assertEquals(&quot;A nice pic&quot;, blob.getDescription(), &quot;Description mismatch for 'blob'&quot;);
</code></pre>
<p>This method will fetch all resources across multiple pages without handling pagination.
If you need more control over pagination, you can use the overloaded <code>listResources(int pageNo)</code> method instead.</p>
<pre><code class="language-java">List&lt;ResourceItem&gt; resourceList = mcpClient.listResources(1);
assertEquals(2, resourceList.size(), &quot;Expected exactly two resources&quot;);
</code></pre>
<h3>Reading Resource Contents</h3>
<p>To read the contents of a resource:</p>
<pre><code class="language-java">GetResourceResult.ResourceResultDetail response = mcpClient.readResource(&quot;file:///text&quot;);
assertEquals(1, response.getContents().size(), &quot;Expected exactly one content&quot;);

ResourceContent contents = response.getContents().get(0);

ResourceContentText textContents = (ResourceContentText) contents;
assertEquals(&quot;file:///text&quot;, textContents.getUri(), &quot;URI should be 'file:///text'&quot;);
assertEquals(&quot;text888&quot;, textContents.getText(), &quot;Text content should be 'text'&quot;);

</code></pre>
<p>The <code>readResource()</code> method returns a <code>ResourceResultDetail</code> containing an array of resource contents.
Each resource content can be either text content or binary content, depending on the resource's MIME type.</p>
<pre><code class="language-java">GetResourceResult.ResourceResultDetail response = mcpClient.readResource(&quot;file:///blob&quot;);
assertEquals(1, response.getContents().size(), &quot;Expected exactly one content&quot;);

ResourceContent contents = response.getContents().get(0);

ResourceContentBinary blobContents = (ResourceContentBinary) contents;
assertEquals(&quot;file:///blob&quot;, blobContents.getUri(), &quot;URI should be 'file:///blob'&quot;);
assertEquals(&quot;blob&quot;, blobContents.getBlob(), &quot;Blob content should be 'blob'&quot;);
</code></pre>
<p>You can read the resource with template, that means, you can pass any parameters to the resource.</p>
<pre><code class="language-java">GetResourceResult.ResourceResultDetail response = mcpClient.readResource(&quot;file:///text-template/hello&quot;);
assertEquals(1, response.getContents().size(), &quot;Expected exactly one content&quot;);

ResourceContent contents = response.getContents().get(0);

ResourceContentText textContents = (ResourceContentText) contents;
assertEquals(&quot;file:///text-template/hello&quot;, textContents.getUri(), &quot;URI should be 'file:///text-template/hello'&quot;);
assertEquals(&quot;text hello&quot;, textContents.getText(), &quot;Text content should be 'text hello'&quot;);
</code></pre>
<!--
### Resource Subscriptions

The client can subscribe to resource changes to receive notifications when resources are updated:

err := client.Subscribe(ctx, mcp.SubscribeRequest{
Params: mcp.SubscribeParams{
URI: "test://resource/path",
},
})

To stop receiving notifications for a resource:

err := client.Unsubscribe(ctx, mcp.UnsubscribeRequest{
Params: mcp.UnsubscribeParams{
URI: "test://resource/path",
},
})

-->

            </article>

            <div></div>
        </div>
        <footer>
            AJ MCP SDK, a part of
            <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a>
            open source. Mail:frank@ajaxjs.com, visit
            <a href="https://blog.csdn.net/zhangxin09" target="_blank">my blog(In Chinese)</a>. <br/> <br/> Copyright © 2025 Frank Cheung. All rights reserved.
        </footer>
    </body>
</html>